package com.example.jianbing.DataStructures;

import ch.qos.logback.core.net.SyslogOutputStream;

import java.util.ArrayList;

/**
 * @ClassName : AllNumbersArranged
 * @Description : N个数字全排列
 * @Author : JB
 * @Date: 2021-01-24 09:38
 */


public class AllNumbersArranged {

    private static int index = 0;
    public static void main(String[] args) {
        String[] strings = {"1","2","3","4","5"};

        ArrayList<String> arrayList = new ArrayList<>();
        if(strings.length==0){
            System.out.println("数组为空");
        }else if(strings.length == 1) {
            System.out.println(strings[0]);
        }else {
            array(strings,arrayList);
        }
    }

    private static void array(String[] strings, ArrayList<String> first_list) {
        ArrayList<String> end_list = new ArrayList<>();

        if(first_list.size()==0) {
            first_list.add(strings[0]+strings[1]);
            first_list.add(strings[1]+strings[0]);
            end_list = first_list;
        }else if(first_list.size()==2&&index==1){
            end_list = first_list;
        }else{
            for(int i=0;i<first_list.size();i++) {
                for(int j=0;j<=first_list.get(i).length();j++) {
                    StringBuilder str1 = new StringBuilder(first_list.get(i));
                    str1.insert(j, strings[index]);
                    end_list.add(str1.toString());
                }
            }
        }
        index++;
        if(index==strings.length) {
            System.out.println(end_list);
            System.out.println(end_list.size());
        }else {
            array(strings,end_list);
        }
    }
}
